<link rel="stylesheet" href="/stylesheets/admin.css">
<style>
    .has-right {
        color: green;
    }
    .none-right {
        color: red;
    }
</style>
<div class="row">
    <div class="col-xs-3">
        <ul class="list-group">
            <li class="list-group-item list-group-title"><strong style="font-size: 16px">管理中心</strong></li>
            <% if (user && user.root) { %>
            <li class="list-group-item"><a class="menu-item" href="/admin/user">用户管理</a></li>
            <li class="list-group-item"><a class="menu-item" href="/admin/role">角色管理</a></li>
            <% } %>
            <li class="list-group-item"><a class="menu-item" href="/admin/robot">机器人管理</a></li>
            <li class="list-group-item active"><a class="menu-item" href="/admin/delegater">代理机管理</a></li>
            <li class="list-group-item"><a class="menu-item" href="/admin/gituser">Git用户管理</a></li>
            <% if (user && user.role && user.role.permissions.managerI) { %>
            <li class="list-group-item"><a class="menu-item" href="/admin/repo">Repo管理</a></li>
            <% } %>
        </ul>
    </div>
    <div class="col-xs-9">
        <div class="setting-div">
            <div class="setting-header-div">
                <button id="create-btn" class="create-btn btn btn-default">添加代理机</button>
                <hr>
            </div>
            <div>
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>代理机名字</th>
                        <th>类型</th>
                        <th>平台</th>
                        <th>IP</th>
                        <th>端口</th>
                        <th>用户名</th>
                        <th>打包次数</th>
                        <th>平均耗时</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="list-body">
                    </tbody>
                </table>
            </div>
            <div class="setting-footer-div">

            </div>
        </div>
        <div style="height: 40px"></div>
    </div>
</div>
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加代理机</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="name" class="col-sm-3 control-label">名字</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="name" placeholder="请输入代理机名字">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="token" class="col-sm-3 control-label">IP</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="ip" placeholder="请输入代理机IP">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="token" class="col-sm-3 control-label">端口</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="port" placeholder="请输入代理机端口，默认22">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="token" class="col-sm-3 control-label">用户名</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="user" placeholder="请输入用户名">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="platform" class="col-sm-3 control-label">类型</label>
                        <div class="col-sm-9">
                            <select class="form-control" id="platform">
                                <option value=1>Mac</option>
                                <option value=2>Linux</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="type" class="col-sm-3 control-label">类型</label>
                        <div class="col-sm-9">
                            <select class="form-control" id="type">
                                <option value=1>iOS</option>
                                <option value=2>Android</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="prefix" class="col-sm-3 control-label">前置脚本</label>
                        <div class="col-sm-9">
                            <textarea class="form-control" id="prefix">

                            </textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="suffix" class="col-sm-3 control-label">后置脚本</label>
                        <div class="col-sm-9">
                            <textarea class="form-control" id="suffix">

                            </textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="submit-btn" class="btn btn-primary">提交</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<link rel="stylesheet" href="/stylesheets/jquery-confirm.min-3.2.3.css">
<script src="/javascripts/jquery-confirm.min-3.1.1.js"></script>
<script language="JavaScript">

    var editId = null;

    $("#create-btn").click(function () {
        $('#myModalLabel').text('添加代理机');
        $('#name').val('');
        $('#ip').val('');
        $('#port').val('');
        $('#user').val('');
        $('#prefix').val('');
        $('#suffix').val('');

        $("#type option").each(function(i,n){
            if($(n).text()=='iOS')
            {
                $(n).attr("selected",true);
            } else  {
                $(n).attr("selected",false);
            }
        })
        $("#platform option").each(function(i,n){
            if($(n).text()=='Mac')
            {
                $(n).attr("selected",true);
            } else  {
                $(n).attr("selected",false);
            }
        })
        $('#myModal').modal('show');
        editId = null;
    })

    function delClick(node) {
        $.confirm({
            title: '确定要删除该代理机？',
            content: '删除后将无法恢复',
            animationBounce: 1.5,
            animation: 'rotateXR',
            closeAnimation: 'rotateYR',
            buttons: {
                ok: {
                    text: "确定",
                    btnClass: 'btn-success',
                    keys: ['enter'],
                    action: function(){
                        var tdNode = node.parentNode.parentNode;
                        var id = tdNode.id;
                        delAction(id)
                    }
                },
                cancel: {
                    text: "取消",
                    action:function(){
                    }
                }
            }
        });
    }

    function delAction(id) {
        $.post('/admin/delegater/del',{id:id},function (data) {
            if (data.code === 0) {
                showSuccess('已删除');
                loadDelegaterList();
            } else {
                showError(data.message);
            }
        });
    }

    function editClick(node) {
        $('#myModalLabel').text('编辑代理机');
        var tdNode = node.parentNode.parentNode;
        var id = tdNode.id;

        $('#name').val(document.getElementById('name-' + id).innerText);
        $('#ip').val(document.getElementById('ip-' + id).innerText);
        $('#port').val(document.getElementById('port-' + id).innerText);
        $('#user').val(document.getElementById('user-' + id).innerText);
        $('#prefix').val(document.getElementById('prefix-' + id).innerText);
        $('#suffix').val(document.getElementById('suffix-' + id).innerText);

        var type = document.getElementById('type-' + id).innerText;

        $("#type option").each(function(i,n){
            if($(n).text()==type)
            {
                $(n).attr("selected",true);
            } else  {
                $(n).attr("selected",false);
            }
        })

        var platform = document.getElementById('platform-' + id).innerText;

        $("#platform option").each(function(i,n){
            if($(n).text()==platform)
            {
                $(n).attr("selected",true);
            } else  {
                $(n).attr("selected",false);
            }
        })

        $('#myModal').modal('show');
        editId = id;
    }

    $("#submit-btn").click(function () {
        var name = $('#name').val();
        var ip = $('#ip').val();
        var port = $('#port').val();
        var user = $('#user').val();
        var prefix = $('#prefix').val();
        var suffix = $('#suffix').val();
        var type = $('#type option:selected').val();
        var platform = $('#platform option:selected').val();


        if (name.trim().length === 0) {
            showError('名称不能为空');
            return;
        }
        if (ip.trim().length === 0) {
            showError('IP不能为空');
            return;
        }
        if (user.trim().length === 0) {
            showError('用户名不能为空');
            return;
        }
        if (type === null || type === 0) {
            type = 1;
        }

        if (platform === null || platform === 0) {
            platform = 1;
        }

        var param = {
            name : name.trim(),
            ip : ip,
            platform : platform,
            user : user.trim(),
            type : type,
            prefix : prefix,
            suffix : suffix
        };
        if (port) {
            param.port = port;
        }

        if (editId) {
            param.id = editId;
            $.post("/admin/delegater/edit",param, function(data) {
                if (data.code === 0) {
                    $('#myModal').modal('hide');
                    showSuccess('已更新代理机 ' + name);
                    loadDelegaterList();
                } else  {
                    showError(data.message);
                }
            });
        } else  {
            $.post("/admin/delegater/add",param, function(data) {
                if (data.code === 0) {
                    $('#myModal').modal('hide');
                    showSuccess('已添加代理机 ' + name);
                    loadDelegaterList();
                } else  {
                    showError(data.message);
                }
            });
        }
    })

    $(function () {
        loadDelegaterList();
    })

    function loadDelegaterList() {
        $.post("/admin/delegater/list",function (data) {
            if (data.code === 0) {
                var parent = document.getElementById('list-body');
                for (var i = parent.children.length - 1; i >= 0; i--) {
                    parent.removeChild(parent.children[i]);
                }
                for (var i = 0 ; i < data.data.length ; i++) {
                    var delegater = data.data[i];
                    var delegaterNode = document.createElement('tr');
                    var id = delegater._id;

                    delegaterNode.id = id;
                    var nameNode = document.createElement('td');
                    nameNode.id = 'name-' + id;
                    nameNode.innerText = delegater.name;

                    var portNode = document.createElement('td');
                    portNode.id = 'port-' + id;
                    portNode.innerText = delegater.port;

                    var ipNode = document.createElement('td');
                    ipNode.id = 'ip-' + id;
                    ipNode.innerText = delegater.ip;

                    var userNode = document.createElement('td');
                    userNode.id = 'user-' + id;
                    userNode.innerText = delegater.user;

                    var timesNode = document.createElement('td');
                    timesNode.innerText = delegater.packageCount;

                    var spendNode = document.createElement('td');
                    spendNode.innerText = delegater.packageSpend;

                    var typeNode = document.createElement('td');
                    typeNode.id = 'type-' + id;
                    typeNode.innerText = delegater.type === 2 ? 'Android' : (delegater.type === 1 ? 'iOS' : '未知');

                    var platformNode = document.createElement('td');
                    platformNode.id = 'platform-' + id;
                    platformNode.innerText = delegater.platform === 2 ? 'Linux' : (delegater.platform === 1 ? 'Mac' : '未知');

                    var prefixNode = document.createElement('td');
                    prefixNode.id = 'prefix-' + id;
                    prefixNode.hidden = 'true';
                    prefixNode.innerText = delegater.prefixScript;

                    var suffixNode = document.createElement('td');
                    suffixNode.id = 'suffix-' + id;
                    suffixNode.hidden = 'true';
                    suffixNode.innerText = delegater.suffixScript;

                    var  editNode = document.createElement('td');
                    editNode.innerHTML = "<a onclick='editClick(this)'>编辑</a>&nbsp;&nbsp;<a onclick='delClick(this)'>删除</a>";

                    delegaterNode.appendChild(nameNode);
                    delegaterNode.appendChild(typeNode);
                    delegaterNode.appendChild(platformNode);
                    delegaterNode.appendChild(ipNode);
                    delegaterNode.appendChild(portNode);
                    delegaterNode.appendChild(userNode);
                    delegaterNode.appendChild(timesNode);
                    delegaterNode.appendChild(spendNode);
                    delegaterNode.appendChild(prefixNode);
                    delegaterNode.appendChild(suffixNode);
                    delegaterNode.appendChild(editNode);

                    parent.appendChild(delegaterNode);
                }

            } else  {

            }
        })
    }
</script>